#include "ContextMenu.h"
#include "BSUtility.h"
#define SetAttr(AttrName) SetAttribute(AttrName, ele->Attribute(AttrName));
void ContextMenu::ParseXmlElement(TiXmlElement* ele)
{
    /*  SetAttr("pos")
      SetAttr("size")
      SetAttr("name")*/
    if (strcmp(ele->Value(), "ContextMenu") == 0)
    {
        SetAttr("name")
            SetAttr("pos")
            SetAttr("size")
            ele = ele->FirstChildElement();
            while (ele)
            {
                Control* ctrl = CreateControl(ele->Value());
                ctrl->rootWindow = this->rootWindow;
                /*if (strcmp(ele->Value(), "Widget") == 0)
                {
                    Widget* wi = (Widget*)ctrl;
                    wi->ParseXmlElement(ele);
                }*/
                ctrl->DoParseXmlElement(ele);
                std::string sCtrlName = "";
                TiXmlAttribute* ta = ele->FirstAttribute();
                while (ta)
                {
                    ctrl->SetAttribute(ta->Name(), ta->Value());
                    /*	if (strcmp(ta->Name(), "pos") == 0)
                        {
                            int x, y;
                            sscanf_s(ta->Value(), "%d,%d", &x, &y);
                            ctrl->pos.x = x;
                            ctrl->pos.y = y;
                        }
                        else if (strcmp(ta->Name(), "size") == 0)
                        {
                            int w, h;
                            sscanf_s(ta->Value(), "%d,%d", &w, &h);
                            ctrl->size.x = w;
                            ctrl->size.y = h;
                        }
                        else if (strcmp(ta->Name(), "name") == 0)
                        {
                            sCtrlName = ta->Value();
                        }*/
                        /*else if (strcmp(ta->Name(), "bkcolor") == 0 || strcmp(ta->Name(), "bkclr") == 0)
                        {
                            sCtrlName = ta->Value();
                            int w, h;
                            ctrl->size.x = w;
                            ctrl->size.y = h;
                        }
                        else if (strcmp(ta->Name(), "hovercolor") == 0 || strcmp(ta->Name(), "hoverclr") == 0)
                        {
                            UITheme::
                        }*/

                    ta = ta->Next();
                }

                if (ctrl)
                {
                    childs.push_back(std::make_pair(sCtrlName, ctrl));
                }
                ele = ele->NextSiblingElement();
            }
    }
}

bool ContextMenu::SetAttribute(const char* attrName, const char* attrVal)
{
    if (attrVal == nullptr) return false;
	if (Control::SetAttribute(attrName, attrVal)) {
		return true;
	}
    if (strcmp(attrName, "name") == 0)
    {
        name.val = attrVal;
    }
    if (strcmp(attrName, "pos") == 0)
    {
        int x, y;
        sscanf_s(attrVal, "%d,%d", &x, &y);
        pos.x = x;
        pos.y = y;
        return true;
    }
    if (strcmp(attrName, "size") == 0)
    {
        int x, y;
        sscanf_s(attrVal, "%d,%d", &x, &y);
        size.x = x;
        size.y = y;
        return true;
    }
    return false;
}

void ContextMenu::OnDraw()
{
    if (!bVisible) return;
    DrawBorder();

    for (int i = 0;i < childs.size();i++)
    {
        childs[i].second->pos.x.val = pos.x.val;
        childs[i].second->pos.y.val = pos.y.val;
        childs[i].second->Draw();
    }
}

void ContextMenu::Show()
{
    bVisible = true;
}

void ContextMenu::onMouseMove(int x, int y)
{
}

void ContextMenu::onMouseButtonDown(int button, int x, int y)
{
   // bVisible = false;
}

void ContextMenu::onMouseButtonUp(int button, int x, int y)
{
    Control* root = childs[0].second;
    for (int i = 0; i < root->childs.size(); i++)
    {
        Control* cld = root->childs[i].second;
        Vec2vbi point;
        point.x.val = x;
        point.y.val = y;
        if (BSUtility::HitTest(point, cld->pos, cld->size).val)
        {
            if (cld->name.val == "optFuncs")
            {
                bVisible = false;
                pos.y.val = -200;
           }
        }
    }
}

void ContextMenu::onKeyDown(int key, bool bCtrl, bool bShift, bool bAlt)
{
    Control* root = childs[0].second;
    root->onKeyDown(key, bCtrl, bShift, bAlt);
   /* for (int i = 0; i < root->childs.size(); i++)
    {
        Control* cld = root->childs[i].second;
        Vec2vbi point;
        point.x.val = x;
        point.y.val = y;
        if (Utility::HitTest(point, cld->pos, cld->size).val)
        {
            if (cld->name.val == "optFuncs")
            {
                bVisible = false;
            }
        }
    }*/
}

void ContextMenu::onKeyUp(int key)
{
    if (key == SDL_SCANCODE_RETURN)
    {
        printf("key:%d\r\n",key);
        onNewFuncNameInput.Invoke(this,"");
        return;
    }

    Control* root = childs[0].second;
    root->onKeyUp(key);
}

Control* ContextMenu::CreateControl(std::string sControlClassName)
{
    Control* ctrl = dynamic_cast<Control*>(BlueprintScriptLibrary::BlueprintDataPool::GetInstance()->createInstFuncs[sControlClassName]());
    ctrl->rootWindow = rootWindow;
    return ctrl;
}
